package com.prawn.user.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.user.dao.DictionaryDao;
import com.prawn.user.pojo.Dictionary;
import util.NullAttributeUtils;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class DictionaryService {

	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Dictionary> findAll() {
		return dictionaryDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Dictionary> findSearch(Map whereMap, int page, int size) {
		Specification<Dictionary> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return dictionaryDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Dictionary> findSearch(Map whereMap) {
		Specification<Dictionary> specification = createSpecification(whereMap);
		return dictionaryDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "dictionary",key = "#id")
	public Dictionary findById(String id) {
		return dictionaryDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param dictionary
	 */
	public void add(Dictionary dictionary) {
		dictionary.setId( idWorker.nextId()+"" );
		dictionaryDao.save(dictionary);
	}

	/**
	 * 修改
	 * @param dictionary
	 */
	@CacheEvict(value = "dictionary",key = "#dictionary.id")
	public void update(Dictionary dictionary) {
		dictionaryDao.save(dictionary);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "dictionary",key = "#id")
	public void deleteById(String id) {
		dictionaryDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Dictionary> createSpecification(Map searchMap) {

		return new Specification<Dictionary>() {

			@Override
			public Predicate toPredicate(Root<Dictionary> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.equal(root.get("name").as(String.class), (String)searchMap.get("name")));
                }
                // 数据值（业务代码）
                if (searchMap.get("value")!=null && !"".equals(searchMap.get("value"))) {
                	predicateList.add(cb.equal(root.get("value").as(String.class), (String)searchMap.get("value")));
                }
                // 标签名
                if (searchMap.get("label")!=null && !"".equals(searchMap.get("label"))) {
                	predicateList.add(cb.like(root.get("label").as(String.class), (String)searchMap.get("label")));
                }
                // 类型
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                	predicateList.add(cb.equal(root.get("type").as(String.class), (String)searchMap.get("type")));
                }
                // 中文类型名
                if (searchMap.get("zhType")!=null && !"".equals(searchMap.get("zhType"))) {
                	predicateList.add(cb.equal(root.get("zhType").as(String.class), (String)searchMap.get("zhType")));
                }
                // 父级编号
                if (searchMap.get("parentId")!=null && !"".equals(searchMap.get("parentId"))) {
                	predicateList.add(cb.equal(root.get("parentId").as(String.class), (String)searchMap.get("parentId")));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
